<?php

namespace App\Http\Controllers\Admin;
use App\AdminUser;
use App\AdminRole;
use App\AdminPermissionRole;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Post;
class UserController extends Controller
{
      /**
     *  增加用户
     */
    //add roles 
    public  function  addUser(){
            $this->validate(request(),[

                    'name'=>'required|unique:admin_users',
                    'password' => 'required'
            ]);
           $arr=[
                'name'=>request('name'),
                'password'=>bcrypt(request('password'))
           ];
           \App\AdminUser::create($arr);
           return back()->withErrors('add user success');
    }

    /*
            add  权限用户
    */
 public function PEstore(){
              $this->validate(request(),[

                    'name'=>'required|unique:admin_permissions',
                    'description' => 'required'
            ]);
            $arr=[

                  'name'=>request('name'),
                'description' => request('description')

            ];
            \App\AdminPermission::create($arr);
            return redirect('/admin/permissions');

 }
 /*
        add role
 */
    public function  REstore(){
            $this->validate(request(),[

                    'name'=>'required|unique:admin_roles',
                    'description' => 'required'
            ]);
            $arr=[

                  'name'=>request('name'),
                'description' => request('description')

            ];
            \App\AdminRole::create($arr);
            return redirect('/admin/roles');
    }
    /*
            角色管理的权限设置
    */
    public function per(AdminRole $role){
        if(count($role->permission )==0){
            $lowPe=[];
        }else{
              foreach ($role->permission as $pe) {
               $lowPe[]=$pe->pivot->permission_id;
            };
        }
       
            $truePermissions=collect(request('permissions'));
                
           foreach ($truePermissions->diff($lowPe) as $pe_id) {
               # code...
                  \App\AdminPermissionRole::create(['permission_id'=>$pe_id,'role_id'=>$role->id]);
                               }
            foreach (collect($lowPe)->diff($truePermissions)  as $pe_id) {
               # code...
                  \App\AdminPermissionRole::where(['permission_id'=>$pe_id,'role_id'=>$role->id])->delete();
                
           }
           return redirect('/admin/roles');
    }
    /*
      修改用户的角色
    */
    
    public function storeRole(AdminUser $user){
             if(count($user->roles)==0){
                     $lowRoles=[];
             }else{
                   foreach ($user->roles as $role) {
               $lowRoles[]=$role->pivot->role_id;
                    };
             };
         
            $trueRoles=collect(request('roles'));

           foreach ($trueRoles->diff($lowRoles)  as $role_id) {
               # code...
                  \App\AdminRoleUser::create(['role_id'=>$role_id,'user_id'=>$user->id]);
                               }
            foreach (collect($lowRoles)->diff($trueRoles)  as $role_id) {
               # code...
                  \App\AdminRoleUser::where(['role_id'=>$role_id,'user_id'=>$user->id])->delete();
                
           }
           return redirect('/admin/users');
           
    }
    /*
       角色管理页面的权限管理
    */
   public function permissionUdate(AdminRole $role){
        
        $permissions=\App\AdminPermission::all();
        if(count($role->permission)==0){
            $arr=[];

        }else{
             foreach ($role->permission as $pe) {
            $arr[]=$pe->pivot->permission_id;
        }
        }
       
        return view('admin.role.permission',compact('permissions','arr','role'));

   }
    public function  rolesCreate()
    {
        //


        return view('admin.role.roles');
    }
    public function permissions()
    {
        //
          $permissions=\App\AdminPermission::paginate(4);

        return view('admin.user.permissions',compact('permissions'));
    }
    public function users()
    {
        // $user=\App\AdminUser::find(1);
        //  $permissions=\App\AdminPermission::all();
        // foreach ($permissions as $permission) {
        //     # code...
                  
        //         var_dump( $user->hasPermission($permission)) ;
                           
        // }
        // exit;

       $adminUsers=AdminUser::get();

        return view('admin.user.users',compact('adminUsers'));
    }
      public function roles()
    {
        //
        $roles=AdminRole::all();
        return view('admin.user.roles',compact('roles'));
    }

        public function posts()
    {
        //
         
          $posts=Post::withoutGlobalScope('status')->where('status',0)->paginate(4);
        
        return view('admin.posts.posts',compact('posts'));
    }
  

    public function postStore(Post $post)
    {
        //
        
        $post->status=request('status');
        $post->save();
        
        return [

                'error'=>0,
        ];
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function permissionsCreate()
    {
        //
      

        return view('admin.add.addpermissions');
    }
    public function usersCreate()
    {
        //
        
        return view('admin.add.addusers');
    }

    /*
        显示用户管理页面的角色管理
    */
    public function roleCreate($id)
    {
        //
        $user=adminUser::find($id);
        if(count($user->roles)==0){
                $arr=[];
        }else{
              foreach ($user->roles as $role) {
               $arr[]=$role->pivot->role_id;
        };
        };
      
        
        $roles=\App\AdminRole::all();
     
        return view('admin.add.addroles',compact('roles','user','arr'));
    }

  
    
    public function store(Request $request)
    {
        //
        $this->validate($request,[
            'name'=>'required|unique:admin_users',
            'password'=>'required',
        ]);
        $arr=[

            'name'=>request('name'),
            'password'=>bcrypt(request('password')),
        ];
        \App\AdminUser::create($arr);
        return redirect('/admin/users');
    }

}
